package com.qiantu.phone.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.UserPushSetsBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.dialog.BottomTerminalsDialog;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


/**
 * @Description: 门铃推送设置
 */

public class DeviceSaveActivity extends AppActivity{
    private RecyclerView rvUserPush;
    private UserPushAdapter userPushAdapter;
    private String deviceSerialNo;
    private BottomTerminalsDialog mBottomTerminalsDialog;
    private List<UserPushSetsBean.UserPushSets> userPushSets = new ArrayList<>();   //全部推送人信息
    private List<UserPushSetsBean.UserPushSets> mUserPushSets = new ArrayList<>();  //需要保存的推送人信息
    private List<UserPushSetsBean.UserPushSets.Terminals> mTerminals = new ArrayList<>();  //选中的终端设备
    private Map<Integer, Boolean> checkBoxMap = new HashMap<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_save;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        deviceSerialNo = intent.getStringExtra("deviceSerialNo");
        rvUserPush = (RecyclerView) findViewById(R.id.rv_userPush);
        initAdapter();
    }

    @Override
    protected void initData() {
        initgetUserPush();
    }


    //查询门铃推送用户终端
    private void initgetUserPush() {
        LLHttpManager.getUserPushSets(this,deviceSerialNo,new HttpCallback<HttpData<UserPushSetsBean>>(this){
            @Override
            public void onStart(Call call) {
                super.onStart(call);
            }

            @Override
            public void onSucceed(HttpData<UserPushSetsBean> result) {
                super.onSucceed(result);
                userPushSets = result.getData().getUserPushSets();
                userPushAdapter.setNewData(userPushSets);
                for (int i = 0; i < userPushSets.size(); i++) {
                    checkBoxMap.put(i,false);
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }

    private void initAdapter() {

        userPushAdapter = new UserPushAdapter();
        rvUserPush.setAdapter(userPushAdapter);
        rvUserPush.setLayoutManager(new LinearLayoutManager(this));
        userPushAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                CheckBox check = view.findViewById(R.id.check);
                showBottomTerminalsDialog(userPushSets.get(position).getTerminals(),check,position);
            }
        });
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        initSave();
    }

    //是否推送
    private void initSave() {
        if (mUserPushSets==null){
            finish();
        }else {
            LLHttpManager.getSave(this,deviceSerialNo,mUserPushSets,new HttpCallback<HttpData<Void>>(this){
                @Override
                public void onStart(Call call) {
                    super.onStart(call);
                }

                @Override
                public void onSucceed(HttpData<Void> result) {
                    super.onSucceed(result);
                    finish();
                }

                @Override
                public void onFail(Exception e) {
                    super.onFail(e);
                }

                @Override
                public void onEnd(Call call) {
                    super.onEnd(call);
                }
            });
        }

    }

    //推送人里的终端
    private void showBottomTerminalsDialog(List<UserPushSetsBean.UserPushSets.Terminals> terminals, CheckBox check, int position){

        mBottomTerminalsDialog = new BottomTerminalsDialog(this,terminals);
        mBottomTerminalsDialog.setDialogClickListener(new BottomTerminalsDialog.DialogClickListener() {
            @Override
            public void onConfirmClick(List<UserPushSetsBean.UserPushSets.Terminals> terminals) {
                if (terminals.size()!=0){
                    check.setChecked(true);
                    checkBoxMap.put(position,true);
                    mUserPushSets.add(userPushSets.get(position));
                }else {
                    check.setChecked(false);
                    checkBoxMap.put(position,false);
                    mUserPushSets.remove(userPushSets.get(position));
                }
                int size = mUserPushSets.size();
                mBottomTerminalsDialog.dismiss();
            }
        });
        new XPopup.Builder(this)
                .dismissOnBackPressed(true)
                .enableDrag(true)
                .asCustom(mBottomTerminalsDialog);

        mBottomTerminalsDialog.show();
    }

    private class UserPushAdapter extends BaseQuickAdapter<UserPushSetsBean.UserPushSets,BaseViewHolder>{

        public UserPushAdapter() {
            super(R.layout.item_userpush);
        }

        @Override
        public void setNewData(@Nullable List<UserPushSetsBean.UserPushSets> data) {
            super.setNewData(data);
        }

        @Override
        public void onBindViewHolder(@NonNull BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
            super.onBindViewHolder(holder, position);
            CheckBox check = holder.getView(R.id.check);
            check.setOnCheckedChangeListener(null);
            check.setChecked(checkBoxMap.get(position));
            check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkBoxMap.put(position,isChecked);
                }
            });
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, UserPushSetsBean.UserPushSets item) {

            holder.setText(R.id.name,item.getUserName());
            holder.setText(R.id.phone,item.getPhone());
            ImageView image = holder.getView(R.id.image);
            GlideApp.with(getContext())
                    .load(item.getAvatar())
                    .placeholder(R.mipmap.img_start_avatarplaceholder)
                    .error(R.mipmap.img_start_avatarplaceholder)
                    .circleCrop()
                    .into(image);

        }
    }

}